/*
 * LoginMenu.java
 *
 * Created on __DATE__, __TIME__
 */

package serverPresentation;

import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import server.ServerData;

public class LoginMenu extends javax.swing.JFrame {

	private static final long serialVersionUID = 1L;

	public LoginMenu() {		
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
		
		initComponents();
		
		Image im = Toolkit.getDefaultToolkit().getImage("images/security-icon.gif");
		this.setIconImage(im);
	}

	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		administratorLabel = new javax.swing.JLabel();
		passLabel = new javax.swing.JLabel();
		administratorBox = new javax.swing.JTextField();
		passwordBox = new javax.swing.JPasswordField();
		submitButton = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("CERTCOP Server Login Menu");
		setName("CERTCOP Server Login Menu");
		setResizable(false);
		addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowOpened(java.awt.event.WindowEvent evt) {
				formWindowOpened(evt);
			}
		});

		administratorLabel.setText("Adminstrator Name");

		passLabel.setText("Password");

		administratorBox.setText("Administrator");

		passwordBox.setText("password");

		submitButton.setText("Submit");
		submitButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				submitButtonMouseClicked(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addGap(49, 49,
																		49)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addGroup(
																						layout.createSequentialGroup()
																								.addComponent(
																										administratorLabel)
																								.addPreferredGap(
																										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																								.addComponent(
																										administratorBox,
																										javax.swing.GroupLayout.PREFERRED_SIZE,
																										88,
																										javax.swing.GroupLayout.PREFERRED_SIZE))
																				.addGroup(
																						layout.createSequentialGroup()
																								.addGap(25,
																										25,
																										25)
																								.addComponent(
																										passLabel)
																								.addPreferredGap(
																										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																								.addComponent(
																										passwordBox,
																										javax.swing.GroupLayout.PREFERRED_SIZE,
																										90,
																										javax.swing.GroupLayout.PREFERRED_SIZE))))
												.addGroup(
														layout.createSequentialGroup()
																.addGap(104,
																		104,
																		104)
																.addComponent(
																		submitButton)))
								.addContainerGap(47, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap(76, Short.MAX_VALUE)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														administratorLabel)
												.addComponent(
														administratorBox,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(passLabel)
												.addComponent(
														passwordBox,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(37, 37, 37).addComponent(submitButton)
								.addGap(52, 52, 52)));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void formWindowOpened(java.awt.event.WindowEvent evt) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
	}

	private void submitButtonMouseClicked(java.awt.event.MouseEvent evt) {
		String administrator = administratorBox.getText();
		String password = String.valueOf(passwordBox.getPassword());

		this.setName("CERTCOP Server Login Menu");

		if (administrator.length() == 0) {
			JOptionPane.showMessageDialog(this,
					"The administrator name cannot be empty.");
			return;
		}

		if (password.length() == 0) {
			JOptionPane.showMessageDialog(this, "The password cannot be empty");
			return;
		}

		if (administrator.compareTo("Administrator") == 0) {
			ServerData serverData = ServerData.getServerData();
			boolean sucess = serverData.init(password);

			if (sucess) {
				JOptionPane.showMessageDialog(this,
						"Login completed successfully!!");
				this.dispose();
			}
		}

		return;
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JTextField administratorBox;
	private javax.swing.JLabel administratorLabel;
	private javax.swing.JLabel passLabel;
	private javax.swing.JPasswordField passwordBox;
	private javax.swing.JButton submitButton;
	// End of variables declaration//GEN-END:variables

}